﻿using System;
using System.Collections.Generic;
using System.Text;

/*
	Todo extract some base functionality
*/
namespace SqlCeDriver
{
	class SqlServerPopupBuilder
	{
		public SqlServerPopupBuilder(SqlCeDataSource driver)
		{
			this.driver = driver;
		}
		// standards used by this and broker
		public static DbViewDriver.CustomMenuItem AddItem(DbViewDriver.CustomMenuItem root, String menuText, DbView.Commands.Cmnd command)
		{
			DbViewDriver.CustomMenuItem newOne = new DbViewDriver.CustomMenuItem(DbViewDriver.CustomMenuItem.ItemType.item, menuText, command.ToString());
			root.AddItem(newOne);
			return newOne;
		}
		public static DbViewDriver.CustomMenuItem AddCustomItem(DbViewDriver.CustomMenuItem root, String menuText, DbView.Command command)
		{
			DbViewDriver.CustomMenuItem newOne = new DbViewDriver.CustomMenuItem(DbViewDriver.CustomMenuItem.ItemType.item, menuText, command.ToXml());
			root.AddItem(newOne);
			return newOne;
		}
		public static void AddSeparator(DbViewDriver.CustomMenuItem root)
		{
			root.AddItem(new DbViewDriver.CustomMenuItem(DbViewDriver.CustomMenuItem.ItemType.separator));
		}
		public static DbViewDriver.CustomMenuItem AddCustomMenuItem(DbViewDriver.CustomMenuItem menu, String text, String commandTag)
		{
			DbView.Command c = Cmd.MakeCommand(commandTag);
			DbViewDriver.CustomMenuItem newOne = new DbViewDriver.CustomMenuItem(DbViewDriver.CustomMenuItem.ItemType.item, text, c.ToXml());
			menu.AddItem(newOne);
			return newOne;
		}

		public static DbViewDriver.CustomMenuItem AddCustomMenuItem(DbViewDriver.CustomMenuItem menu, String text, String commandTag, int extraColumnNumber)
		{
			DbView.Command c = Cmd.MakeCommand(commandTag);
			DbViewDriver.CustomMenuItem newOne = new DbViewDriver.CustomMenuItem(DbViewDriver.CustomMenuItem.ItemType.item, text, c.ToXml());
			menu.AddItem(newOne);
			return newOne;
		}

		public static void AddProfile(DbViewDriver.CustomMenuItem root) { AddItem(root, "Profile", DbView.Commands.Cmnd.Profile); }
		public static void AddShowSql(DbViewDriver.CustomMenuItem root)
		{
			DbViewDriver.CustomMenuItem item = AddItem(root, "Show Sql", DbView.Commands.Cmnd.ShowCommand);
			item.IsRowIndependent = true;
		}
		/*
			standard popup offering of profile/show sql until you think of what else 
			to implement as a popup
		*/
		public static void AppendDefault(DbViewDriver.CustomMenuItem root, bool withSeparator)
		{
			if (withSeparator) AddSeparator(root);
			AddProfile(root);
			AddShowSql(root);
		}

		public DbViewDriver.CustomMenuItem BuildPopupMenu(DbView.Command cmmand)
		{
			DbViewDriver.CustomMenuItem root = new DbViewDriver.CustomMenuItem(DbViewDriver.CustomMenuItem.ItemType.menu);
			// [Specialise: popup menus]
			switch (cmmand.Cmd)
			{
				case DbView.Commands.Cmnd.Tables:
					AddItem(root, "Data", DbView.Commands.Cmnd.TableData);
					AddItem(root, "Fields", DbView.Commands.Cmnd.Fields);
					AddSeparator(root);
					AddItem(root, "Script schema", DbView.Commands.Cmnd.TableSource);
					AddCustomMenuItem(root, "Create Table", Cmd.CreateTable).IsRowIndependent = true;
					AddCustomMenuItem(root, "Copy", Cmd.CopyTable); // see
					AddItem(root, "Export", DbView.Commands.Cmnd.Export);
					AddItem(root, "Import", DbView.Commands.Cmnd.Import);
					AddCustomMenuItem(root, "Script POCO Class", Cmd.ScriptClass);
					AddSeparator(root);
					AddItem(root, "Delete all rows", DbView.Commands.Cmnd.Cleartable);
					AddItem(root, "Remove table", DbView.Commands.Cmnd.Droptable);
					AddSeparator(root);
					AddShowSql(root);
					break;
				case DbView.Commands.Cmnd.TableData:
					AddItem(root, "Profile All", DbView.Commands.Cmnd.Profile);
					this.AddProfileDataOnly(root);
					AddItem(root, "Fields", DbView.Commands.Cmnd.Fields);
					AddItem(root, "Edit view", DbView.Commands.Cmnd.EditView);
					AddSeparator(root);
					this.AddScriptItem(root, "Script data", "DATA");
					AddSeparator(root);
					AddShowSql(root);
					break;
				case DbView.Commands.Cmnd.Fields:
					AddItem(root, "Add", DbView.Commands.Cmnd.AddColumn).IsRowIndependent = true;
					AddItem(root, "Modify", DbView.Commands.Cmnd.ChangeColumn);
					AddItem(root, "Remove", DbView.Commands.Cmnd.RemoveColumn);
					AddCustomMenuItem(root, "Add Foreign Key", Cmd.AddFK); // see
					AppendDefault(root, true);
					break;

				case DbView.Commands.Cmnd.Aggregate:
					this.AddAggregateShowTableData(root, "Show Data");
					AppendDefault(root, true);
					break;
				case DbView.Commands.Cmnd.ForeignKeys:
					AddProfile(root);
					AddCustomMenuItem(root, "Remove", Cmd.DelFK);
					//AddScriptItem(root, "Script", "FK");
					//AddScriptItem(root, "Script all tables", "FKALL");
					//AddScriptItem(root, "Remove", "XFK");
					//AddScriptItem(root, "Remove all", "XFKALL");
					//AddSeparator(root);
					//AddQuickDiffMenuItem(root, Command.Cmd); // see
					AddShowSql(root);
					break;
				case DbView.Commands.Cmnd.TableFieldSearch:
					AddItem(root, "Fields", DbView.Commands.Cmnd.Fields);
					AddSeparator(root);
					AddShowSql(root);
					break;
				/*
								case DbView.Commands.cmd.Views:
									AddItem(root, "View Source", DbView.Commands.cmd.ViewSource);
									AddItem(root, "Data", DbView.Commands.cmd.TableData);
									AddItem(root, "Fields", DbView.Commands.cmd.Fields);
									AddSeparator(root);
									AddItem(root, "Dependencies", DbView.Commands.cmd.Dependencies);
									AddItem(root, "Find in source", DbView.Commands.cmd.SearchSource);
									AddSeparator(root);
									AddQuickDiffMenuItem(root, Command.Cmd); // see
									AddShowSql(root);
									break;
								case DbView.Commands.cmd.Procedures:
									AddItem(root, "View Source", DbView.Commands.cmd.ShowSource);
									AddItem(root, "Dependencies", DbView.Commands.cmd.Dependencies);
									AddItem(root, "Find in source", DbView.Commands.cmd.SearchSource);
									AddSeparator(root);
									AddQuickDiffMenuItem(root, Command.Cmd); // see
									AddShowSql(root);
									break;
								case DbView.Commands.cmd.Functions:
									AddItem(root, "View Source", DbView.Commands.cmd.ShowSource);
									AddItem(root, "Dependencies", DbView.Commands.cmd.Dependencies);
									AddItem(root, "Find in source", DbView.Commands.cmd.SearchSource);
									AddSeparator(root);
									AddQuickDiffMenuItem(root, Command.Cmd); // see
									AddShowSql(root);
									break;
								case DbView.Commands.cmd.Triggers:
									AddItem(root, "View Source", DbView.Commands.cmd.ShowSource);
									AddItem(root, "Dependencies", DbView.Commands.cmd.Dependencies);
									AddSeparator(root);
									AddShowSql(root);
									break;
								case DbView.Commands.cmd.ProcedureSourceSearch:
									AddItem(root, "View Source", DbView.Commands.cmd.ShowSource);
									AddSeparator(root);
									AddShowSql(root);
									break;
								case DbView.Commands.cmd.ViewSourceSearch:
									AddItem(root, "View Source", DbView.Commands.cmd.ShowSource);
									AddSeparator(root);
									AddShowSql(root);
									break;
								case DbView.Commands.cmd.FunctionSourceSearch:
									AddItem(root, "View Source", DbView.Commands.cmd.ShowSource);
									AddSeparator(root);
									AddShowSql(root);
									break;
				*/
				/*
								case DbView.Commands.cmd.Dependencies:
									AddItem(root, "View Source", DbView.Commands.cmd.ShowSource);
									AddCustomMenuItem(root, "Show tree", Cmd.DependencyTree);
									AppendDefault(root, true);
									break;
								case DbView.Commands.cmd.Permissions:
									AddPermissionSummary(root, "Summary");
									AppendDefault(root, true);
									break;
				*/
				// [Specialise: extension command popup menus]
				case DbView.Commands.Cmnd.Extension:
					switch (cmmand.Name)
					{
						default:
							AppendDefault(root, false); break;
					}
					break;
				default:
					AppendDefault(root, false);
					break;
			}
			return root;
		}

		/*
			Custom construction
		*/
		void AddProfileDataOnly(DbViewDriver.CustomMenuItem root)
		{
			DbView.Command command = new DbView.Command(DbView.Commands.Cmnd.Profile);
			command.AddParam("NOTEMPTY", "Y");
			AddCustomItem(root, "Profile Data only", command);
		}

		void AddScriptItem(DbViewDriver.CustomMenuItem root, String menuText, String objectType)
		{
			DbView.Command command = new DbView.Command(DbView.Commands.Cmnd.ShowSource);
			command.AddParam("TYPE", objectType);
			AddCustomItem(root, menuText, command);
		}

		void AddArticlesItem(DbViewDriver.CustomMenuItem root, String menuText)
		{
			DbView.Command command = Cmd.MakeCommand(Cmd.Articles);
			command.AddParam("PUB", "Y");  // flag to use the table parameter
			AddCustomItem(root, menuText, command);
		}

		void AddShowData(DbViewDriver.CustomMenuItem root, String menuText, int targetColumn)
		{
			DbView.Command command = new DbView.Command(DbView.Commands.Cmnd.ShowProfileData, "", DbView.Command.CommandAction.sysaction);
			command.AddParam("DATACOL", targetColumn.ToString());
		}

		void AddAggregateShowTableData(DbViewDriver.CustomMenuItem root, String menuText)
		{
			DbView.Command command = new DbView.Command(DbView.Commands.Cmnd.TableData);
			command.AddParam("CALLER", DbView.Commands.Cmnd.Aggregate.ToString());
			AddCustomItem(root, menuText, command);
		}

		void AddPermissionSummary(DbViewDriver.CustomMenuItem root, String menuText)
		{
			DbView.Command command = new DbView.Command(DbView.Commands.Cmnd.Permissions);
			command.AddParam("SUMMARY", "Y");
			AddCustomItem(root, menuText, command);
		}

		private void AddQuickDiffMenuItem(DbViewDriver.CustomMenuItem root, DbView.Commands.Cmnd callerCmd)
		{
			DbView.Command c = Cmd.MakeCommand(Cmd.QuickDiffDialog);
			c.AddParam("CALLER", callerCmd.ToString());
			AddCustomItem(root, "Quick Diff", c);
		}

		// todo: convert these to Cmd. Strings.
		public DbViewDriver.CustomMenuItem BuildCustomMenu()
		{
			String menuTitle = this.CreateMenuTitle();
			DbViewDriver.CustomMenuItem menu = new DbViewDriver.CustomMenuItem(DbViewDriver.CustomMenuItem.ItemType.menu, menuTitle, "");
			AddCustomMenuItem(menu, "Create Table", Cmd.CreateTable);
			//			AddCustomMenuItem(menu, "Articles", Cmd.Articles);
			return menu;
		}

		private string CreateMenuTitle()
		{
			String version = (this.driver.GetDriverName().IndexOf("3") != -1) ? "3.5" :
							 (this.driver.GetDriverName().IndexOf("4") != -1) ? "4" : "";
			return String.Format("SqlCE {0}", version);
		}

		SqlCeDataSource driver;
	}
}
